/*
 * Created by JFormDesigner on Sat May 16 18:17:48 CEST 2009
 */

package server;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import org.apache.log4j.Logger;
/**
 * @author FreeFab
 */
public class Ui_server extends JFrame{

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Ui_server.class);
	ServerPresentazione rsi = new  ServerPresentazione();
	private String name;
	 


	private void button_AvviaServer(ActionEvent e) {
		try {
			logger.info("Avvio ServerPresentazione Bind ");
			name = rsi.bind();
			button1.setEnabled(false);
			button2.setEnabled(true);
			
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "Connessione non Riuscita\n", "Errore connessione", JOptionPane.ERROR_MESSAGE);
			logger.error("Connessione non riuscita");
			e1.printStackTrace();
			
		
		}
	}

	private void button_StopServer(ActionEvent e) {
		try {
			rsi.unbind();
			button2.setEnabled(false);
			button1.setEnabled(true);
			
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "Sconnessione non Riuscita\n", "Errore connessione", JOptionPane.ERROR_MESSAGE);
			logger.error("Sconnessione non Riuscita");
			e1.printStackTrace();
		}
	}

	

	public  boolean isValidIp(final String ip)
	{
	     return ip.matches("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})");
	}
	
	public  boolean isValidPort(final String port)
	{
	     return port.matches("([0-9]{1,8})");
	}
	
	
	private void button_Salva(ActionEvent e) {
		String ip= textField_Indirizzo.getText();
		String port = textField_Porta.getText();
	
		if(!isValidIp(ip)){
			JOptionPane.showMessageDialog(null, "L'indirizzo IP deve essere formato da 4 numeri decimali", "Configurazione Server", JOptionPane.WARNING_MESSAGE);
			logger.info("L'indirizzo IP deve essere formato da 4 numeri decimali");
		}else if(!isValidPort(port)){
			JOptionPane.showMessageDialog(null, "Porta inserita Errata", "Configurazione Server", JOptionPane.WARNING_MESSAGE);
			logger.info("Porta inserita Errata");
	}else
		{
		rsi.setIp(ip);
		rsi.setPort(port);
		button4.setEnabled(false);
		logger.info("Configurazione server riuscita con Ip: "+ip+ " e porta "+port);
		}
	}
	
	
	private void textField_IndirizzoActionPerformed(ActionEvent e) {
	
	}

	private void textField_PortaActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void logAncestorAdded(AncestorEvent e) {
		
	}

	private void logInputMethodTextChanged(InputMethodEvent e) {
	
	}

	private void textArea1CaretUpdate(CaretEvent e) {
		// TODO add your code here
	}

	public void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Free Fab
		button1 = new JButton();
		button2 = new JButton();
		label_indirizzo = new JLabel();
		textField_Indirizzo = new JTextField();
		label_Porta = new JLabel();
		textField_Porta = new JTextField();
		label_LogSystem = new JLabel();
		button4 = new JButton();
		scrollPane1 = new JScrollPane();
		textArea1 = new JTextArea();
		button3 = new JButton();

		//======== this ========
		setTitle("Server");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- button1 ----
		button1.setText("Avvia Server");
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button_AvviaServer(e);
			}
		});
		contentPane.add(button1);
		button1.setBounds(5, 220, 190, 50);

		//---- button2 ----
		button2.setText("Stop Server");
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button_StopServer(e);
			}
		});
		contentPane.add(button2);
		button2.setBounds(200, 220, 185, 50);

		//---- label_indirizzo ----
		label_indirizzo.setText(" Indirizzo ip:");
		contentPane.add(label_indirizzo);
		label_indirizzo.setBounds(0, 10, 90, 21);

		//---- textField_Indirizzo ----
		textField_Indirizzo.setText("127.0.0.1");
		textField_Indirizzo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_IndirizzoActionPerformed(e);
			}
		});
		contentPane.add(textField_Indirizzo);
		textField_Indirizzo.setBounds(80, 5, 130, textField_Indirizzo.getPreferredSize().height);

		//---- label_Porta ----
		label_Porta.setText(" Porta");
		contentPane.add(label_Porta);
		label_Porta.setBounds(210, 10, 40, 21);

		//---- textField_Porta ----
		textField_Porta.setText("12345");
		textField_Porta.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_PortaActionPerformed(e);
			}
		});
		contentPane.add(textField_Porta);
		textField_Porta.setBounds(250, 5, 60, 25);

		//---- label_LogSystem ----
		label_LogSystem.setText(" Log di Sistema:");
		contentPane.add(label_LogSystem);
		label_LogSystem.setBounds(5, 40, 200, label_LogSystem.getPreferredSize().height);

		//---- button4 ----
		button4.setText("Salva");
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button_Salva(e);
			}
		});
		contentPane.add(button4);
		button4.setBounds(310, 5, 85, 30);

		//======== scrollPane1 ========
		{

			//---- textArea1 ----
			textArea1.addCaretListener(new CaretListener() {
				public void caretUpdate(CaretEvent e) {
					textArea1CaretUpdate(e);
				}
			});
			scrollPane1.setViewportView(textArea1);
		}
		contentPane.add(scrollPane1);
		scrollPane1.setBounds(5, 60, 385, 155);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(null);

		//---- button3 ----
		button3.setText("text");
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Free Fab
	private JButton button1;
	private JButton button2;
	private JLabel label_indirizzo;
	private JTextField textField_Indirizzo;
	private JLabel label_Porta;
	private JTextField textField_Porta;
	private JLabel label_LogSystem;
	private JButton button4;
	private JScrollPane scrollPane1;
	private JTextArea textArea1;
	private JButton button3;
	// JFormDesigner - End of variables declaration  //GEN-END:variables





}
